package corej.course.thread.game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;

class Ball {

    public Ball(Component c) {
        canvas = c;
    }

    public Ball(Component c, Color color) {
        canvas = c;
        this.color = color;
    }

    public void draw(Graphics2D g2) {
        if (color != null)
            g2.setColor(color);
        g2.fill(new Ellipse2D.Double(x, y, XSIZE, YSIZE));
    }

    public void move() {
        x += dx;
        y += dy;
        if (x < 0) {
            x = 0;
            dx = -dx;
        }

        if (x + XSIZE >= canvas.getWidth()) {
            x = canvas.getWidth() - XSIZE;
            dx = -dx;
        }

        if (y < 0) {
            y = 0;
            dy = -dy;
        }

        if (y + YSIZE >= canvas.getHeight()) {
            y = canvas.getHeight() - YSIZE;
            dy = -dy;
        }

        canvas.repaint();

    }

    private Component canvas;

    private Color color;

    private static final int XSIZE = 15;

    private static final int YSIZE = 15;

    private int x = 0;

    private int y = 0;

    private int dx = 2;

    private int dy = 2;

}

class BallThread extends Thread {

    public BallThread(Ball aBall) {
        b = aBall;
    }

    public void run() {

        try {

            for (int i = 1; i <= 1000; i++) {
                b.move();
                sleep(10);
            }
        } catch (InterruptedException iex) {
            iex.printStackTrace();
        }
    }

    private Ball b;

}

class BallCanvas extends JPanel {

    public void add(Ball b) {
        balls.add(b);
    }

    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;

        for (int i = 0; i < balls.size(); i++) {
            Ball b = (Ball) balls.get(i);
            b.draw(g2);
        }

    }

    private ArrayList balls = new ArrayList();

}

@SuppressWarnings("serial")
class BounceFrame extends JFrame {

    public BounceFrame() {

        setSize(WIDTH, HEIGHT);
        setTitle("BounceThread");

        Container contentPane = getContentPane();

        canvas = new BallCanvas();
        contentPane.add(canvas, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();

        // 加入“Start"按钮
        addButton(buttonPanel, "Start", new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                addBall();
                counter++;
            }
        });

        // 加入“Close"按钮
        addButton(buttonPanel, "Close", new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.exit(0);
            }
        });

        contentPane.add(buttonPanel, BorderLayout.SOUTH);

    }

    public void addButton(Container c, String title, ActionListener listener) {
        JButton button = new JButton(title);
        c.add(button);
        button.addActionListener(listener);
    }

    // 点击"Start"按钮的动作
    public void addBall() {
        Ball b = new Ball(canvas, colors[counter % colors.length]);
        canvas.add(b);
        BallThread thread = new BallThread(b);
        thread.start();
    }

    private BallCanvas canvas;

    public static final int WIDTH = 450;

    public static final int HEIGHT = 350;

    private Color[] colors = {Color.blue, Color.magenta, Color.green,
            Color.orange, Color.red, Color.yellow, Color.pink};

    private int counter; // =0 Automatic

}

// 主应用程序
public class BounceThread {

    public static void main(String[] args) {
        JFrame frame = new BounceFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

}
